package com.ruoyi.coupon.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.Logical;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.coupon.domain.TbCoupon;
import com.ruoyi.coupon.domain.TbCouponRecord;
import com.ruoyi.coupon.service.ITbCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;

import java.util.List;


/**
 * 优惠券Controller
 *
 * @author gzc
 * @date 2023-04-24
 */
@RestController
@RequestMapping("/coupon")
public class TbCouponController extends BaseController {
    @Autowired
    private ITbCouponService tbCouponService;

    /**
     * 查询优惠券列表
     */
    //@RequiresPermissions(value = {"coupon:coupon:list","coupon:coupon:commonCoupon"},logical = Logical.OR)
    @GetMapping("/list")
    public TableDataInfo list(TbCoupon tbCoupon) {
        startPage();
        List<TbCoupon> list = tbCouponService.selectTbCouponList(tbCoupon);
        return getDataTable(list);
    }

    /**
     * 导出优惠券列表
     */
    @RequiresPermissions("coupon:coupon:export")
    @Log(title = "优惠券", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbCoupon tbCoupon) {
        List<TbCoupon> list = tbCouponService.selectTbCouponList(tbCoupon);
        ExcelUtil<TbCoupon> util = new ExcelUtil<TbCoupon>(TbCoupon.class);
        util.exportExcel(response, list, "优惠券数据");
    }

    /**
     * 获取优惠券详细信息
     */
    @RequiresPermissions("coupon:coupon:query")
    @GetMapping(value = "/{couponId}")
    public AjaxResult getInfo(@PathVariable("couponId") Long couponId) {
        return success(tbCouponService.selectTbCouponByCouponId(couponId));
    }

    /**
     * 新增优惠券
     */
    @RequiresPermissions("coupon:coupon:add")
    @Log(title = "优惠券", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbCoupon tbCoupon) throws IOException {

        return toAjax(tbCouponService.insertTbCoupon(tbCoupon));
    }

    /**
     * 修改优惠券
     */
    @RequiresPermissions("coupon:coupon:edit")
    @Log(title = "优惠券", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbCoupon tbCoupon) {
        return toAjax(tbCouponService.updateTbCoupon(tbCoupon));
    }

    /**
     * 删除优惠券
     */
    @RequiresPermissions("coupon:coupon:remove")
    @Log(title = "优惠券", businessType = BusinessType.DELETE)
    @DeleteMapping("/{couponIds}")
    public AjaxResult remove(@PathVariable Long[] couponIds) {
        return toAjax(tbCouponService.deleteTbCouponByCouponIds(couponIds));
    }


    /**
     * 领取优惠券
     */
    //  @RequiresPermissions("coupon:coupon:receive")
    @PutMapping(value = "/{couponId}")
    public AjaxResult receiveCoupon(@PathVariable("couponId") Long couponId) throws ParseException {
         return tbCouponService.receiveCoupon(couponId);
    }

    @RequiresPermissions("coupon:coupon:MyCoupon")
    @GetMapping("/myCoupon")
    public TableDataInfo selectMyCoupon(TbCouponRecord tbCouponRecord,TbCoupon tbCoupon){
        startPage();
        return getDataTable(tbCouponService.selectMyTbCouponList(tbCouponRecord,tbCoupon));
    }


}
